package net.huashitong.ssydt.provider.money.api;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.08.14
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huashitong.ssydt.money.entity.vo.*;
import net.huashitong.ssydt.provider.annotations.LoginRequired;
import net.huashitong.ssydt.provider.money.service.MoneyService;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import net.huashitong.ssydt.security.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

@Api(value = "/api/money", tags = {"金币模块"})
@RestController
@RequestMapping("/api/money")
public class ApiMoneyController {

    private MoneyService moneyService;

    @Autowired
    public ApiMoneyController(MoneyService moneyService) {
        this.moneyService = moneyService;
    }

    @ApiOperation(value = "vip套餐列表", notes = "vip套餐列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/vip/bags")
    public Result<ArrayList<VipBagsVo>> vipBags() {
        return ResultUtils.getSuccessResultList(this.moneyService.vipBags());
    }
    @ApiOperation(value = "vip套餐列表-v2", notes = "vip套餐列表-v2", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/vip/{appType}/bags")
    public Result<ArrayList<VipBagsVo>> vipBags2(
            @ApiParam(value = "app类型", required = true) @PathVariable("appType") String appType
            ) {
        return ResultUtils.getSuccessResultList(this.moneyService.vipBagsByAppType(appType));
    }


    @ApiOperation(value = "我的金币", notes = "我的金币", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @GetMapping("/golds/my")
    public Result<GoldsMyVo> goldsMy() {
        String userId = UserUtils.getUserId();
        return ResultUtils.getSuccessResults(moneyService.goldsMy(userId));
    }

    @ApiOperation(value = "金币套餐列表", notes = "金币套餐列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/golds")
    public Result<ArrayList<GoldyVo>> golds() {
        return ResultUtils.getSuccessResultList(this.moneyService.golds());
    }


    @ApiOperation(value = "统一下单", notes = "统一下订单接口", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping("/orders")
    public Result<OrderOutVO> orders(@Valid @RequestBody OrderInVO orderInVO) {
        String userId = UserUtils.getUserId();
        OrderOutVO orderOutVO = moneyService.orders(orderInVO, userId);
        return ResultUtils.getSuccessResults(orderOutVO);
    }


    @ApiOperation(value = "获取我的vip状态", notes = "获取我的vip状态", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @GetMapping("/vips/my")
    public Result<VipMyVO> vipMy() {
        String userId = UserUtils.getUserId();
        VipMyVO vipMyVO = moneyService.vipMy(userId);
        return ResultUtils.getSuccessResults(vipMyVO);
    }

    @ApiOperation(value = "我的金币记录", notes = "我的金币记录", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @GetMapping("/golds/records/my")
    public Result<ArrayList<GoldsRecordsMyOutVO>> goldsRecordsMy(@Valid GoldsRecordsMyInVO goldsRecordsMyInVO) {
        String userId = UserUtils.getUserId();
        List<GoldsRecordsMyOutVO> list = moneyService.goldsRecordsMy(goldsRecordsMyInVO, userId);
        return ResultUtils.getSuccessResultList(list);
    }


    @ApiOperation(value = "发起微信支付", notes = "发起微信支付", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/orders/{orderId}/wechat/pay")
    public Result<TreeMap<String, String>> weChatPay(
            @ApiParam(value = "订单编号", required = true) @PathVariable("orderId") String orderId,
            @ApiParam(value = "request", hidden = true) HttpServletRequest request) {
        TreeMap<String, String> map = moneyService.wechatPay(orderId, request);
        return ResultUtils.getSuccessResults(map);
    }
    @ApiOperation(value = "发起金币支付", notes = "发起金币支付", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping("/orders/{orderId}/gold/pay")
    public Result<String> goldPay(
            @ApiParam(value = "订单编号", required = true) @PathVariable("orderId") String orderId) {
        String userId = UserUtils.getUserId();
        moneyService.goldPay(orderId, userId);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "发起支付宝支付", notes = "发起支付宝支付", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/orders/{orderId}/alipay/pay")
    public Result<String> alipayPay(
            @ApiParam(value = "订单编号", required = true)
            @PathVariable("orderId") String orderId) {
        String orderString = moneyService.alipayPay(orderId);

        return ResultUtils.getSuccessResults(orderString);
    }


    @ApiOperation(value = "验证苹果支付", notes = "验证苹果支付", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping("/orders/apple/pay")
    public Result<String> applePay(@Valid @RequestBody ApplePayVO applePayVO) throws IOException {
        String userId = UserUtils.getUserId();
        moneyService.applePay(applePayVO, userId);
        return ResultUtils.getSuccessResults();
    }

}
